﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using GDF_kaart_inlezen.strategy_classes;
using System.Data;
using ErikEJ.SqlCe;

namespace GDF_kaart_inlezen
{
    class Bibliotheek
    {
        public Dictionary<int, Strategy> types;
        private Thread t1;
        private Reader r;
        DataTable table1 = new DataTable();

        public Bibliotheek(Reader r)
        {

            DataColumn dc = new DataColumn("piet");
            table1.Columns.Add(dc);
            this.r = r;
            types = new Dictionary<int, Strategy>();
            types.Add(24, new Edge());
            types.Add(01, new VolumeHeader());
            types.Add(02, new DataSetHeader());
            types.Add(03, new FieldDefinition());
            types.Add(04, new RecordDefinition());
            types.Add(05, new AtributeDefinition());
            types.Add(07, new FeatureDefinition());
            types.Add(09, new RelationshipDefinition());
            types.Add(15, new DefaultAtributeValue());
            types.Add(18, new AttributeValueDefinition());
            types.Add(61, new Datum_Ellipsoid());
            types.Add(66, new EarthMagneticField());
            types.Add(16, new SectionHeader());
            types.Add(17, new LayerHeader());
            types.Add(90, new Comment());
            types.Add(99, new VolumeTermination());
            types.Add(23, new Coordinates());
            types.Add(25, new NewNode());
            types.Add(29, new Face());
            types.Add(51, new PointFeature());
            types.Add(52, new LineFeature());
            types.Add(53, new AreaFeature());
            types.Add(54, new ComplexFeature());
            types.Add(41, new Name());
            types.Add(44, new SegmentAtrribute());
            types.Add(46, new Conversion());
            types.Add(50, new Relationship());
            types.Add(45, new TimeDomain());
        }

        public void StartThread()
        {
            t1 = new Thread(new ThreadStart(Parse));
            t1.Start();
        }

        public void Parse()
        {
            while (true)
            {
                if (r.queue.Count > 0)
                {
                    try
                    {
                        string line = r.queue[0];
                        r.queue.RemoveAt(0);
                            //int id = Int32.Parse(line.Substring(0, 2));
                            //types[id].process();
                            // string shit = "INSERT INTO tabel (piet) values('" + line + "')";
                            // jizz.execute(shit);
                            table1.Rows.Add("swag swag swag swag swag ");
                    }
                    catch (Exception) { }
                }
                else if (r.reading_done)
                {
                    Console.WriteLine("DONE");
                    /*foreach (DataRow row in table1.Rows)
                    {
                        Console.WriteLine(row[0].ToString());
                    }*/
                  //  DoBulkCopy(false, table1);
                    Console.WriteLine("DONE");
                    this.t1.Abort();

                }
            }

        }

        public void process(string line)
        {
            try
            {
                int id = Int32.Parse(line.Substring(0, 2));
                if (id == 24)
                {
                    //types[id].process();
                }
            }
            catch (Exception e) { Console.WriteLine("Error:" + line); Console.WriteLine(e.Message); }
        }

        public string[] SplitLine(string line)
        {
            string[] res = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            List<string> arrayList = new List<string>(res);

            /*foreach (string s in res)
            {
                if (s != "")
                    arrayList.Add(s);
            }*/

            return arrayList.ToArray();
        }
      
    }
}
